package com.lf.sp.controller.backend;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.google.common.collect.Maps;
import com.lf.sp.common.Const;
import com.lf.sp.common.ResponseCode;
import com.lf.sp.common.ServerResponse;
import com.lf.sp.pojo.Product;
import com.lf.sp.pojo.User;
import com.lf.sp.service.IFileService;
import com.lf.sp.service.IProductService;
import com.lf.sp.service.IUserService;
import com.lf.sp.util.PropertiesUtil;

/**
 * 
 * @author ha
 *
 */
@Controller
@RequestMapping("/manage/product/")
public class ProductManageController {
	@Autowired
	private IUserService iUserService;
	
	@Autowired
	private IProductService iProductService;
	
	@Autowired
	private IFileService iFileService;
	/**
	 * 新增或者更新产品
	 * @param session
	 * @param product
	 * @return
	 */
	@RequestMapping(value="product_save.do",method=RequestMethod.POST)
	@ResponseBody
	public ServerResponse productSave(HttpSession session,Product product){
		User user = (User) session.getAttribute(Const.CURRENT_USER);
		if(user == null){
			return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录请登录");
		}
		if(!iUserService.checkAdminRole(user).isSuccess()){
			return ServerResponse.createByErrorMessage("用户为普通用户，无管理员权限");
		}
//		填充增加产品的业务逻辑
		return iProductService.saveOrUpdateProduct(product);
	}
	/**
	 * 更新产品状态
	 * @param session
	 * @param product
	 * @return
	 */
	@RequestMapping(value="set_sale_status.do",method=RequestMethod.POST)
	@ResponseBody
	public ServerResponse setSaleStatus(HttpSession session ,Integer productId , Integer status){
		User user = (User) session.getAttribute(Const.CURRENT_USER);
		if(user == null){
			return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录请登录");
		}
		if(!iUserService.checkAdminRole(user).isSuccess()){
			return ServerResponse.createByErrorMessage("用户为普通用户，无管理员权限");
		}
//		填充增加产品的业务逻辑
		return iProductService.setSaleStatus(productId, status);
	}
	
	/**
	 * 获取产品详细信息
	 * @param session
	 * @param productId
	 * @return
	 */
	@RequestMapping(value="get_detali.do")
	@ResponseBody
	public ServerResponse getDetail(HttpSession session,Integer productId){
		User user = (User) session.getAttribute(Const.CURRENT_USER);
		if(user == null){
			return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录请登录");
		}
		if(!iUserService.checkAdminRole(user).isSuccess()){
			return ServerResponse.createByErrorMessage("用户为普通用户，无管理员权限");
		}
//		填充增加产品的业务逻辑
		return iProductService.getDetail(productId);
	}
	/**
	 * 分页查询产品列表
	 * @param session
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("get_list.do")
	@ResponseBody
	public ServerResponse getList(HttpSession session,@RequestParam(value="pageNum",defaultValue = "1") int pageNum ,@RequestParam(value = "pageSize" , defaultValue = "10") int pageSize){
		User user = (User) session.getAttribute(Const.CURRENT_USER);
		if(user == null){
			return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录请登录");
		}
		if(!iUserService.checkAdminRole(user).isSuccess()){
			return ServerResponse.createByErrorMessage("用户为普通用户，无管理员权限");
		}
//		填充增加产品的业务逻辑
		return iProductService.getProductList(pageNum, pageSize);
	}
	/**
	 * 后台分页搜索产品信息列表
	 * @param session
	 * @param productName
	 * @param productId
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("product_search.do")
	@ResponseBody
	public ServerResponse productSearch(HttpSession session,String productName , Integer productId ,@RequestParam(value="pageNum",defaultValue = "1") int pageNum ,@RequestParam(value = "pageSize" , defaultValue = "10") int pageSize){
		User user = (User) session.getAttribute(Const.CURRENT_USER);
		if(user == null){
			return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录请登录");
		}
		if(!iUserService.checkAdminRole(user).isSuccess()){
			return ServerResponse.createByErrorMessage("用户为普通用户，无管理员权限");
		}
//		填充增加产品的业务逻辑
		return iProductService.productSearch(productName, productId, pageNum, pageSize);
	}
	/**
	 * SpringMVC文件上传
	 * @param session
	 * @param file
	 * @param request
	 * @return
	 */
	//TODO 未进行测试
	@RequestMapping(value="upload.do")
	@ResponseBody
	public ServerResponse upload(HttpSession session , @RequestParam(value="upload_file",required=false)MultipartFile file,HttpServletRequest request){
		User user = (User) session.getAttribute(Const.CURRENT_USER);
		if(user == null){
			return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录请登录");
		}
		if(!iUserService.checkAdminRole(user).isSuccess()){
			return ServerResponse.createByErrorMessage("用户为普通用户，无管理员权限");
		}
//		填充增加产品的业务逻辑
		String path = request.getSession().getServletContext().getRealPath("upload");
		String targetFileName = iFileService.upload(file, path);
		String url = PropertiesUtil.getProperty("ftp.server.http.prefix")+targetFileName;
		
		Map fileMap = Maps.newHashMap();
		fileMap.put("uri", targetFileName);
		fileMap.put("url",url);
		return ServerResponse.createBySuccess(fileMap);
		
	}
	
	
	
}
